/*
 * Copyright (C) 2011 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.ui;

import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.Session;
import com.mol.market.common.util.TopBar;
import com.mol.market.common.util.Utils;
import com.mol.market.common.widget.BaseActivity;
import com.mol.market.common.widget.SplashView;


/**
 * 更多信息页
 * 
 * @author andrew.wang
 * @date 2011-11-23
 * 
 */
public class MoreInfoActivity extends BaseActivity implements OnClickListener {

    private SplashView mSplashView;
    private RelativeLayout root;
    
    // 检查用户是否更换主题

    private BroadcastReceiver mThemeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TopBar.initSkin(Session.get(context), MoreInfoActivity.this);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        root = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_more_info_layout,
                null, false);

        init();

        setContentView(root);

        initTopBar();
        
        IntentFilter themeFilter = new IntentFilter(Constants.BROADCAST_CHANGE_THEME);
        registerReceiver(mThemeReceiver, themeFilter);
    }

    private void init() {

        LayoutInflater flater = getLayoutInflater();

        LinearLayout ll = new LinearLayout(getApplicationContext());
        ll.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout guide = (RelativeLayout) flater.inflate(R.layout.info_list_item, ll,
                false);
        guide.setId(0);
        guide.setOnClickListener(this);
        ImageView guideLogo = (ImageView) guide.findViewById(R.id.iv_logo);
        guideLogo.setImageResource(R.drawable.guide);
        TextView guideName = (TextView) guide.findViewById(R.id.tv_name);
        guideName.setText(getString(R.string.guide_hint));
        TextView guideAddress = (TextView) guide.findViewById(R.id.tv_description);
        guideAddress.setText(getString(R.string.guide_info));
        guide.findViewById(R.id.indicator).setVisibility(View.VISIBLE);
        ll.addView(guide);
        
        RelativeLayout feedback = (RelativeLayout) flater.inflate(R.layout.info_list_item, ll,
                false);
        feedback.setId(1);
        feedback.setOnClickListener(this);
        ImageView feedbackLogo = (ImageView) feedback.findViewById(R.id.iv_logo);
        feedbackLogo.setImageResource(R.drawable.email);
        TextView feedbackName = (TextView) feedback.findViewById(R.id.tv_name);
        feedbackName.setText(getString(R.string.feedback_hint));
        TextView feedbackAddress = (TextView) feedback.findViewById(R.id.tv_description);
        feedbackAddress.setText(getString(R.string.feedback_email));
        feedback.findViewById(R.id.indicator).setVisibility(View.VISIBLE);
        ll.addView(feedback);

        RelativeLayout phone = (RelativeLayout) flater.inflate(R.layout.info_list_item, ll, false);
        phone.setClickable(true);
        ImageView phoneLogo = (ImageView) phone.findViewById(R.id.iv_logo);
        phoneLogo.setImageResource(R.drawable.phone);
        TextView phoneName = (TextView) phone.findViewById(R.id.tv_name);
        phoneName.setText(getString(R.string.tele_hint));
        TextView phoneAddress = (TextView) phone.findViewById(R.id.tv_description);
        phoneAddress.setText(getString(R.string.tele_number));
        ll.addView(phone);

        RelativeLayout weibo = (RelativeLayout) flater.inflate(R.layout.info_list_item, ll, false);
        weibo.setId(2);
        weibo.setOnClickListener(this);
        ImageView weiboLogo = (ImageView) weibo.findViewById(R.id.iv_logo);
        weiboLogo.setImageResource(R.drawable.weibo);
        TextView qqName = (TextView) weibo.findViewById(R.id.tv_name);
        qqName.setText(getString(R.string.qq_group));
        TextView qqAddress = (TextView) weibo.findViewById(R.id.tv_description);
        qqAddress.setText(getString(R.string.qq_group_id));
        weibo.findViewById(R.id.indicator).setVisibility(View.VISIBLE);
        ll.addView(weibo);

        mSplashView = new SplashView(getApplicationContext(), ll);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
        lp.addRule(RelativeLayout.BELOW, R.id.top_bar);
        mSplashView.setLayoutParams(lp);
        root.addView(mSplashView);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mThemeReceiver);
    }

    /**
     * 初始化Topbar
     */
    private void initTopBar() {
        TopBar.createTopBar(mSession, this, new View[] { findViewById(R.id.top_bar_title),
                findViewById(R.id.top_bar_search) }, new int[] { View.VISIBLE, View.GONE },
                getString(R.string.about));
    }

    @Override
    public void onClick(View arg0) {

        switch (arg0.getId()) {
        case 0:
            // 新手引导
            final Intent guideIntent = new Intent(getApplicationContext(), UserGuideActivity.class);
            guideIntent.putExtra(Constants.EXTRA_ACTION_VIEW, true);
            try {
                startActivity(guideIntent);
            } catch (ActivityNotFoundException e) {
                // no activity can handle this intent
            }
            break;

        case 1:
            // 反馈邮件
            final Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                    new String[] { "gfan.support@mappn.com" });
            Utils.trackEvent(getApplicationContext(), Constants.GROUP_12,
                    Constants.MENU_CLICK_EMAIL);
            try {
                // 2011/2/25 fix bug
                startActivity(emailIntent);
            } catch (ActivityNotFoundException e) {
                // no activity can handle this intent
            }
            break;
            
        case 2:
            // 微博
            Intent webIntent = new Intent();
            webIntent.setAction(Intent.ACTION_VIEW);
            webIntent.setData(Uri.parse("http://weibo.com/gfancn"));
            webIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(webIntent);
            break;

        default:
            break;
        }

    }
}
